

*For Margaret dropbox folders 
global NCREIF "/Users/BeckaBrolinson/Dropbox/NCREIF/data" 
global build 	"$NCREIF/build" 
global analysis "$NCREIF/analysis"
global results 	"$analysis/results"
global figures 	"$analysis/figures" 

*------------------------------------------------------------------------------*
*	Step 4- How Does Util Exp Change After Cert If Rent Changes	   *
*------------------------------------------------------------------------------*
{	
	use "$build/01_annualizeddatawcontrol.dta", replace
	*Generate Normalization of Treatment 
	gen years_pre_post_cert= year-firstyearrated
	*Generate the same measure for uncertified buildings so that I can compare the two 
	gen untreat_year=2009
	replace years_pre_post_cert=year-untreat_year if treat==0 
	*Generate Average Rent and util exp before/ after treatment 
	bysort treat years_pre_post_cert: egen mean_exp=mean(rl_yr_utilpersf)
	bysort treat years_pre_post_cert: egen mean_rent=mean(rl_yr_rentpersf) 

	*Scatter plot with mean lines 
	twoway 		(scatter rl_yr_utilpersf years_pre_post_cert if treat==1, msize(vtiny) mcolor(navy)) /*
			*/ 	(scatter rl_yr_utilpersf years_pre_post_cert if treat==0, msize(vtiny) mcolor(cranberry)) /*
			*/ 	(line mean_exp years_pre_post_cert if treat==1, xlabel(-10(1)10) lcolor(navy) yscale(range(0 2))) /*
			*/ 	(line mean_exp years_pre_post_cert if treat==0, xlabel(-10(1)10) lcolor(cranberry) yscale(range(0 2)))
	
	
	
	preserve
	bysort treat years_pre_post_cert: gen id_unique=_n==1 
	keep if id_unique==1 
	keep if years_pre_post_cert >=-10 
	keep if years_pre_post_cert <=10 
	twoway 	(line mean_exp years_pre_post_cert if treat==1, xlabel(-10(1)10) ///
	lcolor(navy) yscale(range(0 10)) ) ///
	(line mean_exp years_pre_post_cert if treat==0, xlabel(-10(1)10) ///
	lcolor(gs10) yscale(range(0 10)) ) ///
	(line mean_rent years_pre_post_cert if treat==0, xlabel(-10(1)10) ///
	lcolor(gs10) lpattern(dash)) ///
	(line mean_rent years_pre_post_cert if treat==1, xlabel(-10(1)10, angle(30)) ///
	lcolor(cranberry) lpattern(dash) yaxis(2) ///
	xline(0, lpattern(dot) lcolor(gs10)) ///
	ylabel(0[1]10, axis(1)) ///
	xtitle("Time Before and After Treatment") ///
	ytitle("Utility Expenditure Per Sf ($)") ytitle("Rent Per Sf ($)", axis(2)) ///
	legend(lab(1 "Certified Utility Exp") ///
	lab(2 "Uncertified Utility Exp") lab(3 "Uncertified Rent") ///
	lab(4 "Certified Rent")) bgcolor(white) graphregion(color(white))) 
	gr export "$figures/01_Rent&UtilExp.png", replace
	restore
	
	preserve
	bysort treat years_pre_post_cert: gen id_unique=_n==1 
	keep if id_unique==1 
	keep if years_pre_post_cert >=-10 
	keep if years_pre_post_cert <=10 
	twoway 	(line mean_exp years_pre_post_cert if treat==1, xlabel(-10(1)10) ///
	lcolor(navy) ) ///
	(line mean_rent years_pre_post_cert if treat==1, xlabel(-10(1)10, angle(30)) ///
	lcolor(cranberry) lpattern(dash) yaxis(2) ///
	xline(0, lpattern(dot) lcolor(gs10)) ///
	xtitle("Time Before and After Treatment") ///
	ytitle("Utility Expenditure Per Sf ($)") ytitle("Rent Per Sf ($)", axis(2)) ///
	legend(lab(1 "Util Exp Per Sf.") ///
	lab(2 "Rent Per Sf.")) title("Certified Buildings (Unmatched)") bgcolor(white) graphregion(color(white))) 
	gr export "figures/02_Rent&UtilExpCert.png", replace
	restore
	
	preserve
	bysort treat years_pre_post_cert: gen id_unique=_n==1 
	keep if id_unique==1 
	keep if years_pre_post_cert >=-10 
	keep if years_pre_post_cert <=10 
	twoway 	(line mean_exp years_pre_post_cert if treat==0, xlabel(-10(1)10) ///
	lcolor(navy) ) ///
	(line mean_rent years_pre_post_cert if treat==0, xlabel(-10(1)10, angle(30)) ///
	lcolor(cranberry) lpattern(dash) yaxis(2) ///
	xline(0, lpattern(dot) lcolor(gs10)) ///
	xtitle("Time Before and After Treatment") ///
	ytitle("Utility Expenditure Per Sf ($)") ytitle("Rent Per Sf ($)", axis(2)) ///
	legend(lab(1 "Util Exp Per Sf.") ///
	lab(2 "Rent Per Sf.")) title("Uncertified Buildings (Unmatched)") bgcolor(white) graphregion(color(white))) 
	gr export "$figures/03_Rent&UtilExpCert.png", replace
	restore
	
	*Graph of rent for uncertified and certified buildings together 
	preserve
	bysort treat years_pre_post_cert: gen id_unique=_n==1 
	keep if id_unique==1 
	keep if years_pre_post_cert >=-10 
	keep if years_pre_post_cert <=10 
	twoway 	(line mean_rent years_pre_post_cert if treat==1, xlabel(-10(1)10) ///
	lcolor(navy) ) ///
	(line mean_rent years_pre_post_cert if treat==0, xlabel(-10(1)10, nogrid angle(30)) ///
	lcolor(cranberry) lpattern(dash) yaxis(1) ///
	xline(0, lpattern(dot) lcolor(gs10)) ///
	xtitle("Years Before and After Treatment") ///
	ytitle("Rent Per Sqft ($)")  ylabel(, nogrid) ///
	legend(rowgap(*1) size(*1) symysize(*1) pos(2) ring(0) bmargin(0 0 0 0) lab(1 "Certified Buildings")  ///
	lab(2 "Uncertified Buildings")) bgcolor(white) graphregion(color(white))) 
	gr export "$figures/04_rentpersf_unmatched.png", replace
	restore
	
	*Graph of utility expenditure for uncertified and certified buildings together 
	preserve
	bysort treat years_pre_post_cert: gen id_unique=_n==1 
	keep if id_unique==1 
	keep if years_pre_post_cert >=-10 
	keep if years_pre_post_cert <=10 
	twoway 	(line mean_exp years_pre_post_cert if treat==1, xlabel(-10(1)10) ///
	lcolor(navy) ) ///
	(line mean_exp years_pre_post_cert if treat==0, xlabel(-10(1)10, nogrid angle(30)) ///
	lcolor(cranberry) lpattern(dash) yaxis(1) ///
	xline(0, lpattern(dot) lcolor(gs10)) ///
	xtitle("Years Before and After Treatment") ylabel(, nogrid) ///
	ytitle("Utility Expenditure Per Sf ($)")  ///
	legend(rowgap(*1) size(*1) symysize(*1) pos(2) ring(0) bmargin(0 0 0 0) lab(1 "Certified Buildings") ///
	lab(2 "Uncertified Buildings")) bgcolor(white) graphregion(color(white))) 
	gr export "$figures/05_utilpersf_unmatched.png", replace
	restore
	
	***Same Graphs but with matched sample 
		
	forvalues i=2/3{
	use "$build/FakeNCREIFAnnualizedDatawcontrol_PostMatch`i'.dta", replace
	
	*Generate Normalization of Treatment 
	gen years_pre_post_cert= year-firstyearrated
	*Generate the same measure for uncertified buildings so that I can compare the two 
	gen untreat_year=2009
	replace years_pre_post_cert=year-untreat_year if treat==0 
	*Generate Average Rent and util exp before/ after treatment 
	bysort treat years_pre_post_cert: egen mean_exp=mean(rl_yr_utilpersf)
	bysort treat years_pre_post_cert: egen mean_rent=mean(rl_yr_rentpersf) 
	
		
	*Graph of rent for uncertified and certified buildings together 
	preserve
	bysort treat years_pre_post_cert: gen id_unique=_n==1 
	keep if id_unique==1 
	keep if years_pre_post_cert >=-10 
	keep if years_pre_post_cert <=10 
	twoway 	(line mean_rent years_pre_post_cert if treat==1, xlabel(-10(1)10) ///
	lcolor(navy) ) ///
	(line mean_rent years_pre_post_cert if treat==0, xlabel(-10(1)10, nogrid angle(30)) ///
	lcolor(cranberry) lpattern(dash) yaxis(1) ///
	xline(0, lpattern(dot) lcolor(gs10)) ///
	xtitle("Years Before and After Treatment") ///
	ytitle("Rent Per Sqft ($)")  ylabel(, nogrid) ///
	legend(rowgap(*1) size(*1) symysize(*1) pos(2) ring(0) bmargin(0 0 0 0) legend(lab(1 "Certified Buildings") ///
	lab(2 "Uncertified Buildings")) bgcolor(white) graphregion(color(white))) 
	gr export "$figures/06_rentpersf_matched`i'.png", replace
	restore
	
	*Graph of utility expenditure for uncertified and certified buildings together 
	preserve
	bysort treat years_pre_post_cert: gen id_unique=_n==1 
	keep if id_unique==1 
	keep if years_pre_post_cert >=-10 
	keep if years_pre_post_cert <=10 
	twoway 	(line mean_exp years_pre_post_cert if treat==1, xlabel(-10(1)10) ///
	lcolor(navy) ) ///
	(line mean_exp years_pre_post_cert if treat==0, xlabel(-10(1)10, nogrid angle(30)) ///
	lcolor(cranberry) lpattern(dash) yaxis(1) ///
	xline(0, lpattern(dot) lcolor(gs10)) ///
	xtitle("Years Before and After Treatment") ///
	ytitle("Utility Expenditure Per Sf ($)") ylabel(, nogrid) ///
	legend(rowgap(*1) size(*1) symysize(*1) pos(2) ring(0) bmargin(0 0 0 0) lab(1 "Certified Buildings")  ///
	lab(2 "Uncertified Buildings")) bgcolor(white) graphregion(color(white))) 
	gr export "$figures/07_utiltpersf_matched`i'.png", replace
	restore
	}
}	

	***Same Graphs but with matched sample but pulling out FE and other controls 
	use "$build/01_annualizeddatawcontrol.dta", replace

	gen sqft1000s = sqft / 1000 
	gen HDD1000s = HDD / 1000 
	gen CDD1000s = CDD / 1000 
	gen used_space1000s = used_space/ 1000
	gen real_capex_ti_sqft = real_capex_ti / sqft 
	gen real_capex_bldimp_sqft = real_capex_bldimp / sqft 
	*set treatment date as 2009 for the untreated buildings 
	replace firstyearrated = 2009 if treat == 0 
	
	gen years_pre_post_cert= year-firstyearrated
	
	gen post_1 = (years_pre_post_cert>=-1) 
	
	gen interaction_1 = post_1 * treat 

	
	set more off 
	*List of covariates 
	#d ;
	global Cov0 	""; 
	global Cov1 	"Covered_E real_elecprice real_gasprice Unemployment HDD1000s CDD1000s 
	age age2 used_space1000s
	 percentleased real_capex_ti_sqft real_capex_bldimp_sqft "; 
	global Cov2 	"Covered_E real_elecprice real_gasprice Unemployment HDD1000s CDD1000s
	 age age2 percentleased used_space1000s
	dFundType1 dFundType2 dFundType4 dFundType5 dFundType6";
	global Cov3	"Covered_E real_elecprice Unemployment HDD1000s CDD1000s 
	age age2 used_space1000s"; 
	#d cr 
	
	capture drop de* resid*
	*eststo RentFECov1 : reghdfe logrealrentpersf interaction $Cov1, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa) residuals(residuals1)
	
	eststo RentFECov1 : reghdfe logrealrentpersf $Cov1, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa) residuals(residuals1)
		
	
	*Generate Average Rent and util exp before/ after treatment 
	bysort treat years_pre_post_cert: egen mean_residuals1=mean(residuals1)
	
	*Graph of rent for uncertified and certified buildings together 
	preserve
	bysort treat years_pre_post_cert: gen id_unique=_n==1 
	keep if id_unique==1 
	keep if years_pre_post_cert >=-7 
	keep if years_pre_post_cert <=7
	twoway 	(line mean_residuals1 years_pre_post_cert if treat==1, xlabel(-10(1)10) ///
	lcolor(navy) ) ///
	(line mean_residuals1 years_pre_post_cert if treat==0, xlabel(-7(1)7, nogrid angle(30)) ///
	lcolor(cranberry) lpattern(dash) yaxis(1) ///
	xline(0, lpattern(dot) lcolor(gs10)) ///
	xtitle("Years Before and After Treatment") ///
	ytitle("Residualized Rent Per Sqft ($)")  ylabel(, nogrid) ///
	legend(pos(6) bmargin(0 0 0 0) lab(1 "Certified Buildings")  ///
	lab(2 "Uncertified Buildings")) bgcolor(white) graphregion(color(white))) 
	gr export "$figures/04_rentpersf_unmatchedresiduals.png", replace
	restore
	
	
	eststo UtilFECov1: reghdfe logrealutilpersf rl_yr_rentpersf $Cov1 , absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa) residuals(residuals2)
	
	*Generate Average Rent and util exp before/ after treatment 
	bysort treat years_pre_post_cert: egen mean_residuals2=mean(residuals2)
	
	*Graph of rent for uncertified and certified buildings together 
	preserve
	bysort treat years_pre_post_cert: gen id_unique=_n==1 
	keep if id_unique==1 
	keep if years_pre_post_cert >=-7
	keep if years_pre_post_cert <=7
	twoway 	(line mean_residuals2 years_pre_post_cert if treat==1, xlabel(-10(1)10) ///
	lcolor(navy) ) ///
	(line mean_residuals2 years_pre_post_cert if treat==0, xlabel(-7(1)7, nogrid angle(30)) ///
	lcolor(cranberry) lpattern(dash) yaxis(1) ///
	xline(0, lpattern(dot) lcolor(gs10)) ///
	xtitle("Years Before and After Treatment") ///
	ytitle("Residualized Utility Bill Per Sqft ($)")  ylabel(, nogrid) ///
	legend(rowgap(*1) size(*1) symysize(*1) pos(2) ring(0) bmargin(0 0 0 0) lab(1 "Certified Buildings")  ///
	lab(2 "Uncertified Buildings")) bgcolor(white) graphregion(color(white))) 
	gr export "$figures/04_utilpersf_unmatchedresiduals.png", replace
	restore
	
	
	*Load in the matched data with the pairs for the match years 
	use "$build/FakeNCREIFAnnualizedDatawcontrol_PostMatch2_wmatches.dta", clear 

	*Generate the post-variable for uncertified buildings to line up with the certification year of their
	*matched building 
	*within each pair generate the treatment year 
	*check that observations are unique 
	capture drop id_unique 
	bysort  matchcalindextreated treat year: gen id_unique = _n ==1 // al unique 
	drop id_unique

	gen sqft1000s = sqft / 1000 
	gen HDD1000s = HDD / 1000 
	gen CDD1000s = CDD / 1000 
	gen used_space1000s = used_space/ 1000
	gen real_capex_ti_sqft = real_capex_ti / sqft 
	gen real_capex_bldimp_sqft = real_capex_bldimp / sqft 
	*set treatment date as treatment year of the matched pair for the untreated buildings 
	by matchcalindextreated: egen firstyearrated_pair = min(firstyearrated)
	gen post_pair = (year>=firstyearrated_pair)
	
	
	gen years_pre_post_cert= year-firstyearrated_pair 
	
	gen post_1 = (years_pre_post_cert>=-1) 
	
	gen interaction_1 = post_1 * treat 

	
	set more off 
	*List of covariates 
	#d ;
	global Cov0 	""; 
	global Cov1 	"Covered_E real_elecprice real_gasprice Unemployment HDD1000s CDD1000s 
	age age2 used_space1000s
	 percentleased real_capex_ti_sqft real_capex_bldimp_sqft "; 
	global Cov2 	"Covered_E real_elecprice real_gasprice Unemployment HDD1000s CDD1000s
	 age age2 percentleased used_space1000s
	dFundType1 dFundType2 dFundType4 dFundType5 dFundType6";
	global Cov3	"Covered_E real_elecprice Unemployment HDD1000s CDD1000s 
	age age2 used_space1000s"; 
	#d cr 
	
	capture drop de* resid*
	
	eststo RentFECov1: reghdfe logrealrentpersf $Cov1 , absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa) residuals(residuals1)
	
	*Generate Average Rent and util exp before/ after treatment 
	bysort treat years_pre_post_cert: egen mean_residuals1=mean(residuals1)
	
	*Graph of rent for uncertified and certified buildings together 
	preserve
	bysort treat years_pre_post_cert: gen id_unique=_n==1 
	keep if id_unique==1 
	keep if years_pre_post_cert >=-7
	keep if years_pre_post_cert <=7 
	twoway 	(line mean_residuals1 years_pre_post_cert if treat==1, xlabel(-10(1)10) ///
	lcolor(navy) ) ///
	(line mean_residuals1 years_pre_post_cert if treat==0, xlabel(-7(1)7, nogrid angle(30)) ///
	lcolor(cranberry) lpattern(dash) yaxis(1) ///
	xline(0, lpattern(dot) lcolor(gs10)) ///
	xtitle("Years Before and After Treatment") ///
	ytitle("Residualized Rent Per Sqft ($)")  ylabel(, nogrid) ///
	legend(pos(6)  bmargin(0 0 0 0) lab(1 "Certified Buildings")  ///
	lab(2 "Uncertified Buildings")) bgcolor(white) graphregion(color(white))) 
	gr export "$figures/04_rentpersf_matchedresiduals.png", replace
	restore
	
	eststo UtilFECov1: reghdfe logrealutilpersf rl_yr_rentpersf $Cov1 , absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa) residuals(residuals2)
	
	*Generate Average Rent and util exp before/ after treatment 
	bysort treat years_pre_post_cert: egen mean_residuals2=mean(residuals2)
	
	*Graph of rent for uncertified and certified buildings together 
	preserve
	bysort treat years_pre_post_cert: gen id_unique=_n==1 
	keep if id_unique==1 
	keep if years_pre_post_cert >=-7 
	keep if years_pre_post_cert <=7
	twoway 	(line mean_residuals2 years_pre_post_cert if treat==1, xlabel(-10(1)10) ///
	lcolor(navy) ) ///
	(line mean_residuals2 years_pre_post_cert if treat==0, xlabel(-7(1)7, nogrid angle(30)) ///
	lcolor(cranberry) lpattern(dash) yaxis(1) ///
	xline(0, lpattern(dot) lcolor(gs10)) ///
	xtitle("Years Before and After Treatment") ///
	ytitle("Residualized Utility Bill Per Sqft ($)")  ylabel(, nogrid) ///
	legend(pos(6) bmargin(0 0 0 0) lab(1 "Certified Buildings")  ///
	lab(2 "Uncertified Buildings")) bgcolor(white) graphregion(color(white))) 
	gr export "$figures/04_utilpersf_matchedresiduals.png", replace
	restore
	
